#!/bin/bash

echo "📊 服务器状态检查..."

# 检查端口3000占用情况
echo "🔍 检查端口3000:"
if lsof -i:3000 > /dev/null 2>&1; then
    echo "✅ 端口3000被占用"
    lsof -i:3000
else
    echo "❌ 端口3000未被占用"
fi

echo ""
echo "🔍 检查NestJS进程:"
NEST_PROCESSES=$(ps aux | grep -E "(nest\.js|nest start)" | grep -v grep)
if [ ! -z "$NEST_PROCESSES" ]; then
    echo "✅ 发现NestJS进程:"
    echo "$NEST_PROCESSES"
else
    echo "❌ 没有发现NestJS进程"
fi

echo ""
echo "🔍 检查npm进程:"
NPM_PROCESSES=$(ps aux | grep "npm run start:dev" | grep -v grep)
if [ ! -z "$NPM_PROCESSES" ]; then
    echo "✅ 发现npm进程:"
    echo "$NPM_PROCESSES"
else
    echo "❌ 没有发现npm进程"
fi

echo ""
echo "🔍 检查PM2状态:"
pm2 list

echo ""
echo "🌐 测试API连接:"
if curl -s http://localhost:3000/api/auth/send-captcha -X POST -H "Content-Type: application/json" -d '{"phone":"13800138000","type":"register"}' > /dev/null 2>&1; then
    echo "✅ API接口正常"
else
    echo "❌ API接口异常"
fi
